package com.qingke.Qiximybatis;

import com.qingke.Qiximybatis.command.Command;
import com.qingke.Qiximybatis.command.CommandFactory;
import com.qingke.Qiximybatis.command.CommandFactory.CommandCode;
import com.qingke.Qiximybatis.sqlclass.MyUser;

public class QixiApp {
	
	private static QixiApp instance = null;
	private QixiApp() {}

	public static synchronized QixiApp getInstance() {
		if (instance == null) {
			instance = new QixiApp();
	    }
			return instance;
	    }
	private MyUser User;
	
	public static void main( String[] args ){
		QixiConsole.println("***************** 欢迎来到七夕不寂寞网上交友平台  *****************");
        while (true) {
            String cmd = QixiConsole.askUserInput("cmd >");
            
            CommandCode cmdCode = null;
            try {
                cmdCode = CommandCode.valueOf(cmd.toUpperCase());
            } catch (Exception e) {
            	QixiConsole.println("你输入了一个不合法的命令，请使用 Help 查看帮助");
                continue;
            }
            CommandFactory factory = CommandFactory.getFactory(QixiApp.getInstance().getUser());
            Command command = factory.buildCommand(cmdCode);
            if (command == null) {
            	QixiConsole.println("请先登陆！");
                continue;
            }
            command.execute();
        }
	}

    public MyUser getUser() {
		return User;
	}

	public void setUser(MyUser user) {
		User = user;
	}

	public static void setInstance(QixiApp instance) {
		QixiApp.instance = instance;
	}
	
}

//    public static void action(){		//不用工厂模式
//    	QixiConsole.println("***************** 欢迎来到七夕不寂寞网上交友平台  *****************");
//    	QixiConsole.println(" ");
//    	try {
//			Thread.sleep(1000);
//		} catch (InterruptedException e1) {
//			e1.printStackTrace();
//		}
//    	QixiConsole.println("您可以通过输入【HELP】获取信息！   或者输入【EXIT】退出登录");
//		String input = null;
//		while(true){
//			input = QixiConsole.askUserInput("cmd >");
//			if(!input.equalsIgnoreCase("exit")&&!input.equalsIgnoreCase("help")){
//				System.out.println("输入的不符合要求内容，请重新输入");
//			}
//			if(input.equalsIgnoreCase("exit")||input.equalsIgnoreCase("help")){
//				break;
//			}
//		}
//		try {
//			int accept = QixiDao.command(input);	
//			QixiDao.profession(accept);	
//			
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//    }